package com.android.gl2jni.filter;

import android.content.Context;
import android.opengl.GLES20;

/**
 * Create time: 2018/9/27.
 */
public class ShineWhiteFilterProgram extends TextureFilterProgram {
  private static final String FRAGMENT =
      "#extension GL_OES_EGL_image_external : require\n" +
      "precision mediump float;\n" +
      "varying vec2 vTextureCoord;\n" +
      "uniform samplerExternalOES uTexture;\n" +
      "uniform float uAdditionalColor;\n" +
      "void main(){\n" +
      "    vec4 color = texture2D(uTexture,vTextureCoord);\n" +
      "    gl_FragColor = vec4(color.r + uAdditionalColor,color.g + uAdditionalColor,color.b + uAdditionalColor,color.a);\n" +
      "}";
  private int mAdditionColorLoc;

  private int mFrames;

  private int mMaxFrames = 8;

  private int mHalfFrames = mMaxFrames / 2;

  public ShineWhiteFilterProgram(Context context) {
    super(context, ProgramType.TEXTURE_EXT);
  }

  @Override
  protected String getFragmentShaderSourceEXT() {
    return FRAGMENT;
  }

  @Override
  protected void initProgram() {
    super.initProgram();
    mAdditionColorLoc = getUniformLocation("uAdditionalColor");
  }

  @Override
  protected void onDraw(int firstVertex, int vertexCount) {
    float progress;
    if (mFrames <= mHalfFrames) {
      progress = mFrames * 1.0f / mHalfFrames;
    } else {
      progress = 2.0f - mFrames * 1.0f / mHalfFrames;
    }
    mFrames++;
    if (mFrames > mMaxFrames) {
      mFrames = 0;
    }
    setFloat(mAdditionColorLoc, progress);
    super.onDraw(firstVertex, vertexCount);
  }
}
